<!--    公用头部  start  -->
<div class="public-header text-center">
    <a class="pull-left" href="home.html">
        <img class="icon-left" src="images/icon-left.png">
    </a>
    <span class="order-tai">找回密码</span>
</div>
<!--    公用头部  end  -->

<div class="text-center margin-top"> <img class="logo" src="images/logo.png"> </div>
<div id="reg-phone" class="my-container" style="display: block;">
    <form id="form1" name="form1" method="post" action="" onsubmit="return validate();">
        <div class="form-group">
            <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></span>
                <input name="phone" id="phone" type="tel" class="form-control input-lg" placeholder="已注册手机号码">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <input name="verify" id="verify" type="tel" maxlength="6" class="form-control input-lg" placeholder="验证码">
		  <span class="input-group-addon" style="padding:0 5px">
		  		<img id="verify_img" style="cursor:pointer;" src="" width="100px">
 		  </span>
            </div>
        </div>
        <div class="form-group">
            <input type="hidden" value="verify_phone" name="action">
            <input type="submit" name="submit" id="submit" value="下一步" class="btn btn-warning btn-lg btn-block">
        </div>
    </form>
</div>
<div id="reg-setpassword" class="my-container" style="">
    <form method="post" action="" onsubmit="return regsetpass();">
        <div style="text-align:center; padding-bottom:10px; padding-top:15px;">我们给你发送了一条短信，请将短信中的数字作为验证码填写在下面。</div>
        <div class="form-group">
            <div class="input-group">
                <input name="phonecode" id="phonecode" type="tel" maxlength="6" class="form-control input-lg" placeholder="验证码">
		  <span class="input-group-addon" style="padding: 0 12px">
		  	<input type="button" id="send_sms" class="btn btn-default btn-sm" style="width:100px" value="剩余32秒" disabled="disabled">
		  </span>
            </div>
        </div>
        <div class="form-group">
            <input type="password" name="password" id="password" class="form-control input-lg" placeholder="请设置密码">
        </div>
        <div class="">
            <input type="hidden" value="" id="reUrl" name="reUrl">
            <input type="hidden" value="verify_setpassword" name="action">
            <input type="hidden" name="phone" id="phone" class="form-control input-lg" value="" placeholder="请填写手机号码">
            <button type="submit" class="btn btn-warning btn-block btn-lg">下一步</button>
        </div>
    </form>
</div>
<div class="my-container">
    <div class="row">
        <div class="col-xs-6 text-left"><a href="login.html">登陆</a></div>
        <div class="col-xs-6 text-right"><a href="reg.html">注册</a></div>
    </div>
</div>
<!-- endbuild -->
<script>
    $('#send_sms').click(function(e) {
        ajaxReg();
    });
    function validate() {

        //return true;
        if ($('#phone').val() == '') {
            myUi.msgbox('请输入手机号!',function(){
                $('#phone').focus();
            });
            return false;
        }
        var reg = /1[3-8]+\d{9}/;
        if(!reg.test($('#phone').val() )){
            myUi.msgbox('无效手机号!',function(){
                $('#phone').focus();
            });
            return false;
        }
        ajaxReg();

        return	false;

    }

    var btnBack = 0;
    function showSetPass(flag) {
        if(flag) {
            $('#reg-phone').slideUp('fast',function() {
                $('#reg-setpassword').slideDown('fast');
                $('#reg-header').html("填写验证码");
                //start_sms_button($('#send_sms'));

                if ($('#btn-back').length < 1) {
                    $('#my-bar-reg').prepend('<a id="btn-back" href="javascript:history.go(-1);" class="my-bar-btn pull-left"><span class="icon-chevron-left"></span></a>');
                }else {
                    btnBack = 1;
                }

                $('#btn-back').click(function() {
                    clearTimeout(timer);
                    showSetPass(0);
                    return false;
                });
            });
        }else {
            $('#reg-setpassword').slideUp('fast',function() {
                $('#reg-phone').slideDown('fast');
                $('#reg-header').html("验证手机号");
                if (btnBack) {
                    $('#btn-back').unbind('click');
                }else {
                    $('#btn-back').remove();
                }
            });

        }

    }


    function regsetpass(){
        if($('#phonecode').val() == '' ) {
            myUi.msgbox('请输入验证码!',function(){
                $('#phonecode').focus();
            });
            return false;
        }
        if($('#password').val() == '' ) {
            myUi.msgbox('请输入验证码!',function(){
                $('#password').focus();
            });
            return false;
        }
        if($('#password').val().length < 6) {
            myUi.msgbox('请输入超过6位密码!',function(){
                $('#password').focus();
            });
            return false;
        }

        $.ajax({
            url: _APP + '/u/reg',
            data: "phone=" + $('#phone').val() + "&phonecode=" + $('#phonecode').val() + "&password=" + $('#password').val() + "&action=verify_setpassword" ,
            dataType:'json',
            type:'post',
            error:function(){
                myUi.msgbox('失败!');
                return false;
            },
            success: function(msg){
                var status = msg.status;
                if(status != '1'){
                    myUi.msgbox('验证码填写错误!');

                    return false;
                }else {
                    window.location.href =$('#reUrl').val();
                }
            }
        });
        return false;
    }

    var timer;
    function countdown(num,obj) {
        if (num >0) {
            num--;
            $(obj).val('剩余'+num+'秒');
            $(obj).attr('disabled',"disabled");
            timer = setTimeout(function(){
                countdown(num,obj);
            },1000);
        }else{
            $(obj).attr('disabled',false);
            $(obj).val('发送验证码');
        }
    }

    function start_sms_button(obj){
        obj.attr('disabled',"disabled");
        var count = 1 ;
        var sum = 60;
        var i = setInterval(function(){
            if(count > 60){
                obj.attr('disabled',false);
                obj.val('发送验证码');
                clearInterval(i);
            }else{
                obj.val('剩余'+parseInt(sum - count)+'秒');
            }
            count++;
        },1000);
    }




    function ajaxReg () {
        $.ajax({
            url: _APP + '/u/reg',
            data: "phone=" + $('#phone').val() +"&action=verify_phone" +"&verify=" + $('#verify').val(),
            dataType:'json',
            type:'post',
            error:function(){
                myUi.msgbox('失败!');
                return false;
            },
            success: function(msg){
                var status = msg.status;
                if(status == '-1'){
                    myUi.msgbox('手机号码不符合规范!');
                    return false;
                }else
                if(status == '-2'){
                    myUi.msgbox('手机号已被注册!');
                    return false;
                }else
                if(status == '-3'){
                    myUi.msgbox('操作频繁，请一分钟之后再试!');
                    return false;
                }else
                if(status == '-4'){
                    myUi.msgbox('手机验证码发送失败!');
                    return false;
                }else
                if(status == '-6'){
                    myUi.msgbox('验证码不正确!',function(){
                        $('#verify_img').attr("src",_APP + '/u/verify?t='+Math.random());
                        $('#verify').val('').focus();
                    });
                    return false;
                }
                if(status == '1'){
                    showSetPass(1);
                    countdown(60,'#send_sms');
                    return true;
                }
            }
        });
    }//ajaxReg()
</script>
